package com.cuihanyu.newuniversityover.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cuihanyu.newuniversityover.MyImgLoader;
import com.cuihanyu.newuniversityover.activity.HomeDetailActivity;
import com.cuihanyu.newuniversityover.adapter.HomeRecyclerviewAdapter;
import com.cuihanyu.newuniversityover.present.MVPPresent;
import com.cuihanyu.newuniversityover.R;
import com.cuihanyu.newuniversityover.util.MyLogUtil;
import com.cuihanyu.newuniversityover.view.MVPView;
import com.cuihanyu.newuniversityover.base.BaseFragment;
import com.cuihanyu.newuniversityover.bean.HomeInfoBean;
import com.cuihanyu.newuniversityover.internet.GlobalContent;
import com.cuihanyu.newuniversityover.util.GsonUtil;
import com.cuihanyu.newuniversityover.views.DividerGridItemDecoration;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;


/**
 * Created by 崔汉宇 on 2018/2/1.
 */

public class Home_Fragment extends BaseFragment implements MVPView {
    private View view;
    private RecyclerView mHome_RecyclerView;
    List<HomeInfoBean.TableBean> homeinfolist = new ArrayList<>();
    private Banner banner;
    private HomeRecyclerviewAdapter mAdapter;
    private MVPPresent mvpPresent;
    private List<Integer> images;
    private Intent intent;
    private ImageView mBack;
    private TextView mTitle;

    @Override
    public View initView() {
        intent = new Intent();
        view = View.inflate(activity, R.layout.fragment_home, null);
        mBack = view.findViewById(R.id.back_ImageView);
        mBack.setVisibility(View.INVISIBLE);
        mTitle = view.findViewById(R.id.title_tv);
        mTitle.setText("精品微课");
        banner = view.findViewById(R.id.banner);
        mHome_RecyclerView = view.findViewById(R.id.Home_RecyclerView);
        mHome_RecyclerView.setLayoutManager(new GridLayoutManager(activity, 2));
        mHome_RecyclerView.addItemDecoration(new DividerGridItemDecoration(activity));
        mvpPresent = new MVPPresent();
        mvpPresent.attachView(this);
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setBannerAnimation(Transformer.CubeOut);
        //设置图片加载器
        banner.setImageLoader(new MyImgLoader());
        //设置图片集合
        getImages();
        banner.setImages(images);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合（当banner样式有显示title时）
        // banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(3500);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        banner.startAutoPlay();
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        if (!isNetworkConnected(activity)) {
            Toast.makeText(activity, "没有网络", Toast.LENGTH_SHORT).show();
        } else {
            mvpPresent.requestData(GlobalContent.HOME_INFO);
        }
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        Toast.makeText(activity, "数据出错:" + e.toString(), Toast.LENGTH_LONG).show();
        MyLogUtil.e("111111111111onError", e.toString());
    }

    @Override
    public void onResponse(String response, int id) {
        MyLogUtil.e("111111111111onResponse", response.toString());
        homeinfolist.clear();
        HomeInfoBean bean = GsonUtil.GsonToBean(response, HomeInfoBean.class);
        homeinfolist.addAll(bean.getTable());
        homeinfolist.addAll(bean.getTable());
        mAdapter = new HomeRecyclerviewAdapter(activity, homeinfolist);
        mHome_RecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                intent.setClass(activity, HomeDetailActivity.class);
                intent.putExtra("Name", homeinfolist.get(position).getVideo_Name());
                startActivity(intent);
            }
        });
    }

    private void getImages() {
        //资源文件
        if (images == null) images = new ArrayList<>();
        images.add(R.drawable.banner1);
        images.add(R.drawable.banner2);
        images.add(R.drawable.banner3);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mvpPresent.detachView();
    }
}
